package com.ittao.taoaicodemother.core.saver;

import com.ittao.taoaicodemother.ai.model.HtmlCodeResult;
import com.ittao.taoaicodemother.ai.model.MultiFileCodeResult;
import com.ittao.taoaicodemother.exception.BusinessException;
import com.ittao.taoaicodemother.exception.ErrorCode;
import com.ittao.taoaicodemother.model.enums.CodeGenTypeEnum;
import com.ittao.taoaicodemother.utils.ApplicationContextProvider;
import org.springframework.stereotype.Component;

/**
 * 代码文件保存执行器
 * 根据代码生成类型执行相应的保存逻辑
 */
@Component
public class CodeFileSaverExecutor {

    /**
     * 执行代码保存器
     *
     * @param codeResult  代码结果对象
     * @param codeGenType 代码生成类型
     * @param appId       应用 ID
     * @param userId      用户 ID
     * @return 保存的目录结果
     */
    public static CodeSaveResult executeSaver(Object codeResult, CodeGenTypeEnum codeGenType, Long appId, Long userId) {
        return switch (codeGenType) {
            case HTML -> {
                HtmlCodeFileSaverTemplate htmlCodeFileSaver = ApplicationContextProvider.getBean(HtmlCodeFileSaverTemplate.class);
                yield htmlCodeFileSaver.saveCode((HtmlCodeResult) codeResult, appId, userId);
            }
            case MULTI_FILE -> {
                MultiFileCodeFileSaverTemplate multiFileCodeFileSaver = ApplicationContextProvider.getBean(MultiFileCodeFileSaverTemplate.class);
                yield multiFileCodeFileSaver.saveCode((MultiFileCodeResult) codeResult, appId, userId);
            }
            default -> throw new BusinessException(ErrorCode.SYSTEM_ERROR, "不支持的代码生成类型: " + codeGenType);
        };
    }
}
